
 var businessLogic = require('../../../utils/NetWork/Model/NewsbusinessLogic.js')
var WxParse = require('../../../utils/Component/wxParse/wxParse.js');
var common = require('../../../utils/common.js')
var orgid = common.getStorageSync("orgid")
var userid = common.getStorageSync("userid") 
function getBannerData() {
  var arr = []
  return arr
}
/*
 * 首页 navnav 数据
 */
 function getIndexNavData() {
  var arr = []
  return arr
} 

  function getIndexNavData1() {
  var arr = []
  return arr
} 
/*
 * 首页 对应 标签 数据项
 */
 function getIndexNavSectionData() {
  var arr = [
    [],[],[],[],[] 
  ]
  return arr
}

  var user_data = userData()
function searchAddrFromAddrs(addrid) {
  var result
  for (let i = 0; i < user_data.addrs.length; i++) {
    var addr = user_data.addrs[i]
    if (addr.addrid == addrid) {
      result = addr
    }
  }
  return result || {}
}  
/**
 * 用户数据
 * */
  function userData() {
   var arr = {
     
   }
  return arr
}  

module.exports = {
  getBannerData: getBannerData,
  getIndexNavData: getIndexNavData,
   getIndexNavSectionData: getIndexNavSectionData, 
  userData: userData,
  searchAddrFromAddrs: searchAddrFromAddrs,
   getIndexNavData1: getIndexNavData1,

}

Page({
  // 页面初始数据
  data: {
    banner_url: [],
    liebiao:[],
    caidan:[],
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    interval: 3000,
    duration: 1000,
    swiperCurrent: 0,
    num: 1,
    // nav 初始化
    navTopItems:getIndexNavData(),
   navTopItem: getIndexNavData1(), 
     navSectionItems: getIndexNavSectionData(), 
    curNavId: 1,
    curIndex: 0,
    openPicker: false,
    down: "none",
    Select: 1,

  },
  swiperChange: function (e) {
    this.setData({
      swiperCurrent: e.detail.current
    })
  },
  onPickHeaderClick: function () {
    this.setData({
      openPicker: !this.data.openPicker,
      // needAnimation: true
    })
     if (this.data.openPicker == false) {
      this.setData({
        down: "none"
      })
    } else {
      this.setData({
        down: "block"
      })
    } 
  },
  
  onPageClick: function () {
    if (this.data.openPicker == true) {
      this.setData({
        openPicker: false,
        down: "none"
        // needAnimation: false
      })
    }
  },

  swiperChange: function (e) {
    this.setData({
      swiperCurrent: e.detail.current
    })
  },switchTab: function (e) {
    common.showLoading("加载中", "loading");
    let id = e.currentTarget.dataset.id,
    index = parseInt(e.currentTarget.dataset.index)
    this.curIndex = parseInt(e.currentTarget.dataset.index)
    var that = this
      this.setData({
        curNavId: id,
        curIndex: index,
      })
      if (id=="0001"){
        this.lunbo();
      }else{
        // console.log("index",this.data.curIndex);
        //common.showToast("加载中", "loading");
        let params = { "page": 1, "num": 5, "orgid": orgid, "userid": userid, "searchField": "channel", "searchValue": e.currentTarget.dataset.channel }
        //第三步 调用接口
        businessLogic.NewsList(params, (res) => { 
           console.log(res)
          var n = [];
          for (var i = 0; i < res.infos.length; i++) {
            if (res.infos[i].onclick > 999999) {
              // n[i] = Math.round((res[i].onclick / 100000) * 100) / 100;
              res.infos[i].onclick = 999999 + "+";
            } else {
              res.infos[i].onclick = res.infos[i].onclick+"次";
            }
          }
          //第五步 把获取的值，传入data
          this.setData({
            liebiao: res.infos ,
           // lenpage: res.page.pages
          });
          common.hideLoading();
        });
      }
      
  },
  // 跳转至详情页

  newsDetailPic: function (event) {
    //跳转
    wx.navigateTo({
      url: "../newsDetailPic/newsDetailPic?id="+ event.currentTarget.dataset.id
    }); 
  },
  orderList: function () {
    //跳转
    wx.navigateTo({
      url: "../orderList/orderList"
    });
  },

  // book
  bookTap: function (e) {
    wx.navigateTo({
      url: '../book/book?aid=' + e.currentTarget.dataset.aid
    })
  },
  // 订阅
  subscription: function () {
    //跳转
    wx.navigateTo({
      url: "../orderList/orderList"
    });
  },



  onLoad: function (options) {
    var scrollHeight = scrollHeight;
    wx.getSystemInfo({
      success: function (res) {
        scrollHeight = res.windowHeight
      }
    });
    this.setData({
      scrollHeight: scrollHeight,
    });
    this.lunbo();
    this.cai();
    this.title();
  },

  bindDownLoad: function () {
    if (this.data.lenpage > this.data.num) {
       var di = this.data.num;
      var di = di + 1; 
      this.setData({
        num: di
      });
      this.lunbo();
    } else {
      common.showToast("到底了", "loading");
    }
    //common.showToast("到底了", "loading");

  },
  scroll: function (event) {
    //该方法绑定了页面滚动时的事件，我这里记录了当前的position.y的值,为了请求数据之后把页面定位到这里来。

  },
  topLoad: function (event) {
    //  该方法绑定了页面滑动到顶部的事件，然后做上拉刷新
    
  }, 

  onShow: function (options){
   // this.lunbo();
    this.cai();
    this.title();
  },
  title :function(){

    let params = { "id": orgid, "orgid": orgid, "userid": userid}
    //第三步 调用接口
    businessLogic.AllInfo(params, (res) => {
      
      wx.setNavigationBarTitle({
        title: res.title//页面标题为路由参数
      })
     

    });

    
  },lunbo: function () {
    // 第二步 传参
    common.showLoading("加载中", "loading");
    let params = { "page": this.data.num, "num": 5, "orgid": orgid, "userid": userid, "searchField": "orgid", "searchValue":orgid}
    //第三步 调用接口
    businessLogic.NewsList(params, (res) => {
      //第五步 把获取的值，传入data
      var n = [];
      for (var i = 0; i < res.infos.length; i++) {
        if (res.infos[i].onclick > 999999) {
          // n[i] = Math.round((res[i].onclick / 100000) * 100) / 100;
          res.infos[i].onclick = 999999 + "+";
        } else {
          res.infos[i].onclick = res.infos[i].onclick + "次";
        }
      }
       var bbb = this.data.liebiao;
      for (var a = 0; a < res.infos.length; a++) {
        bbb.push(res.infos[a]);
      } 
      this.setData({
        banner_url: res.infos,
        liebiao:bbb,
        lenpage: res.page.pages
      });
      common.hideLoading();
    });
  },
  //2.1.1
  cai: function () {
    // 第二步 传参
    let params = {
      "page": 1, "num": 1000, "orgid": orgid, "userid": userid, searchField: "people,school", searchValue: "" + userid+","+orgid+""}
    //第三步 调用接口
    businessLogic.TakeList(params, (res) => {
      // console.log(res);
      var zui ={};
      zui = {
        school: "bdb82a42-a08f-4917-9be4-bc9a4177346f",channel:"001",channel_channel_title:"最新",id:"0001",people:"630acd44-85d0-4792-98e7-5f1a037721f0"
,school:"bdb82a42-a08f-4917-9be4-bc9a4177346f",school_agencyinformation_title:"华东师范附属小学"};
      res.unshift(zui);
      //第五步 把获取的值，传入data
      this.setData({
        caidan: res
      });
  

    });

  },
  errorFunction: function () {
    
  }


  
  
 
})

